package cn.com.hhrcw.service.impl;

import cn.com.hhrcw.entity.*;
import cn.com.hhrcw.mapstruct.*;
import cn.com.hhrcw.service.*;
import cn.com.hhrcw.vo.*;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: recruit-boot-parent
 *
 * @author: JD
 * @create: 2020-11-03 15:56
 **/
@Service
public class EnterpriseServiceImpl implements EnterpriseService {

    @Autowired
    IPiInfoService piInfoService;

    @Autowired
    ICvResumeService resumeService;

    @Autowired
    private CvResumeConvert enCvResumeConvert;

    @Autowired
    private ICvWorkIntentionService cvWorkIntentionService;

    @Autowired
    private CvWorkIntentionConvert enCvWorkIntentionConvert;

    @Autowired
    private ICvUserWorkInfoService cvUserWorkInfoService;

    @Autowired
    private CvUserWorkInfoConvert enCvUserWorkInfoConvert;

    @Autowired
    private ICvUserEduInfoService cvUserEduInfoService;

    @Autowired
    private CvUserEduInfoConvert enCvUserEduInfoConvert;

    @Autowired
    private ICvProjectInfoService cvProjectInfoService;

    @Autowired
    private CvProjectInfoConvert enCvProjectInfoConvert;

    @Autowired
    private ICvTrainService cvTrainService;

    @Autowired
    private CvTrainConvert enCvTrainConvert;

    @Autowired
    private ICvLanguageLevelService cvLanguageLevelService;

    @Autowired
    private CvLanguageLevelConvert enCvLanguageLevelConvert;

    @Autowired
    private ICvSkillsService cvSkillsService;

    @Autowired
    private CvSkillsConvert skillsConvert;

    @Autowired
    private ICvCertificateService cvCertificateService;

    @Autowired
    private CvCertificateConvert certificateConvert;

    @Autowired
    private IFileService fileService;

    @Autowired
    private ICvFamilyMemberService cvFamilyMemberService;

    @Autowired
    private CvFamilyMemberConvert cvFamilyMemberConvert;

    @Override
    public ResumeInfoVO getResumeInfo(String resumeId) {
        ResumeInfoVO result = new ResumeInfoVO();
        LambdaUpdateWrapper<CvResume> wrapper=Wrappers.lambdaUpdate();
        wrapper.eq(CvResume::getId, resumeId)
                .eq(CvResume::getDelFlag, false);

        CvResume cvResume = resumeService.getOne(wrapper);
        if (cvResume == null) {
            return new ResumeInfoVO();
        }
        String userId = cvResume.getUserId();

        LambdaQueryWrapper<PiInfo> infoQuery = new LambdaQueryWrapper<>();
        infoQuery.eq(PiInfo::getDelFlag, false).eq(PiInfo::getUserId, userId);
        PiInfo info = piInfoService.getOne(infoQuery);

        //个人基本信息
        UserInfoVO userInfo = new UserInfoVO();

        // 2021年5月27日15:39:11 增加新的参数  朱修轩
        userInfo.setNation(info.getR2());
        userInfo.setHeight(info.getR3());
        userInfo.setWeight(info.getR4());
        userInfo.setLicense(info.getR5());
        userInfo.setIdCard(info.getR6());
        userInfo.setEmergencyContact(info.getR7());
        userInfo.setEmergencyContactNumber(info.getR8());
        //****************************************


        userInfo.setEMail(info.getEmail());
        userInfo.setLocation(info.getLiveCity());
        userInfo.setName(info.getName());
        userInfo.setSex(info.getSex());
        userInfo.setUserId(info.getUserId());
        userInfo.setJobTime(info.getJobTime());
        userInfo.setWorkYears(info.getWorkYears());
        if (StrUtil.isNotBlank(info.getLiveCity())){
            String[] split1 = info.getLiveCity().split(",");
            userInfo.setLiveCityCode(split1);
        }
        if (info.getBirthday() != null){
            userInfo.setAge(DateUtil.ageOfNow(info.getBirthday()));
        }


        userInfo.setLiveCity(info.getLiveCity());
        userInfo.setBirthday(info.getBirthday());


        if (StrUtil.isNotBlank(info.getHomeTown())){
            String[] split = info.getHomeTown().split(",");
            userInfo.setHomeTownCode(split);
        }

        userInfo.setIdEntity(info.getIdEntity());
        userInfo.setWorkState(info.getWorkState());
        userInfo.setPhotoId(info.getPhotoId());
        userInfo.setPhone(info.getPhone());
        userInfo.setMaritalStatus(info.getMaritalStatus());
        userInfo.setPolitical(info.getPolitical());
        userInfo.setJobFlag(info.getJobFlag());
        userInfo.setUpdateTime(info.getUpdateTime());
        userInfo.setPartyTime(info.getPartyTime());
        userInfo.setForeignLanguage(info.getForeignLanguage());
        userInfo.setComputerSkill(info.getComputerSkill());
        userInfo.setAddress(info.getAddress());
        result.setUserInfo(userInfo);

        //基本信息
        CvResumeVO resumeVO = enCvResumeConvert.toVO(cvResume);
        resumeVO.setEnclosureUrl(resumeVO.getEnclosureId());
        result.setResumes(resumeVO);

        // 家庭成员
        List<CvFamilyMember> list = cvFamilyMemberService.lambdaQuery().eq(CvFamilyMember::getDelFlag, false)
            .eq(CvFamilyMember::getResumeId, resumeId).orderByDesc(CvFamilyMember::getId).list();
        List<CvFamilyMemberVO> cvFamilyMemberVos = cvFamilyMemberConvert.toVO(list);
        result.setFamilyMembers(cvFamilyMemberVos);

        //期望职位
        LambdaQueryWrapper<CvWorkIntention> workIntentionQuery = new LambdaQueryWrapper<>();
        workIntentionQuery.eq(CvWorkIntention::getDelFlag, false).eq(CvWorkIntention::getResumeId, resumeId)
            .orderByDesc(CvWorkIntention::getId);

        List<CvWorkIntention> workIntentions = cvWorkIntentionService.list(workIntentionQuery);

        List<CvWorkIntentionVO> workIntentionVos = enCvWorkIntentionConvert.toVO(workIntentions);
        if (CollectionUtils.isNotEmpty(workIntentionVos)) {
            for (CvWorkIntentionVO vo : workIntentionVos) {
                if (StrUtil.isNotBlank(vo.getCityId())) {
                    String[] strings = vo.getCityId().split(",");
                    vo.setCityStringCode(strings);
                }
                if (StrUtil.isNotBlank(vo.getWorkNature())) {
                    String[] workCode = vo.getWorkNature().split(",");
                    vo.setWorkNatureCode(workCode);
                }
                if (StrUtil.isNotBlank(vo.getWorkIndustry())) {
                    String[] industry = vo.getWorkIndustry().split(",");
                    vo.setWorkIndustryCode(industry);
                }

            }
        }
        result.setWorkIntentions(workIntentionVos);

        //工作实习经历
        LambdaQueryWrapper<CvUserWorkInfo> userWorkInfoQuery = new LambdaQueryWrapper<>();
        userWorkInfoQuery.eq(CvUserWorkInfo::getDelFlag, false).eq(CvUserWorkInfo::getResumeId, resumeId)
            .orderByDesc(CvUserWorkInfo::getWorkStartTime);

        List<CvUserWorkInfo> userWorkInfos = cvUserWorkInfoService.list(userWorkInfoQuery);
        List<CvUserWorkInfoVO> infoVos = enCvUserWorkInfoConvert.toVO(userWorkInfos);
        if (CollectionUtils.isNotEmpty(infoVos)) {
            for (CvUserWorkInfoVO vo : infoVos) {
                if (StrUtil.isNotBlank(vo.getWorkSign())) {
                    String[] strings = vo.getWorkSign().split(",");
                    vo.setWorkSignCode(strings);
                }
                if (StrUtil.isNotBlank(vo.getPositionName())) {
                    String[] split2 = vo.getPositionName().split(",");
                    vo.setPositionNameCode(split2);
                }

            }
        }
        result.setUserWorkInfos(infoVos);

        //教育经历
        LambdaQueryWrapper<CvUserEduInfo> userEduInfoQuery = new LambdaQueryWrapper<>();
        userEduInfoQuery.eq(CvUserEduInfo::getDelFlag, false).eq(CvUserEduInfo::getResumeId, resumeId)
        .orderByDesc(CvUserEduInfo::getStartEduTime);
        List<CvUserEduInfo> userEduInfos = cvUserEduInfoService.list(userEduInfoQuery);
        result.setUserEduInfos(enCvUserEduInfoConvert.toVO(userEduInfos));

        //项目经历
        LambdaQueryWrapper<CvProjectInfo> projectInfoQuery = new LambdaQueryWrapper<>();
        projectInfoQuery.eq(CvProjectInfo::getDelFlag, false).eq(CvProjectInfo::getResumeId, resumeId)
                .orderByDesc(CvProjectInfo::getStartTime);
        List<CvProjectInfo> projectInfos = cvProjectInfoService.list(projectInfoQuery);
        result.setProjectInfos(enCvProjectInfoConvert.toVO(projectInfos));

        //培训经历
        LambdaQueryWrapper<CvTrain> trainQuery = new LambdaQueryWrapper<>();
        trainQuery.eq(CvTrain::getDelFlag, false).eq(CvTrain::getResumeId, resumeId)
        .orderByDesc(CvTrain::getStartTime);
        List<CvTrain> trainInfos = cvTrainService.list(trainQuery);
        result.setTrainInfos(enCvTrainConvert.toVO(trainInfos));

        //语言水平
        LambdaQueryWrapper<CvLanguageLevel> languageLevelQuery = new LambdaQueryWrapper<>();
        languageLevelQuery.eq(CvLanguageLevel::getDelFlag, false).eq(CvLanguageLevel::getResumeId, resumeId)
                .orderByDesc(CvLanguageLevel::getId);
        List<CvLanguageLevel> languageLevelInfos = cvLanguageLevelService.list(languageLevelQuery);
        result.setLanguageLevelInfos(enCvLanguageLevelConvert.toVO(languageLevelInfos));

        //职业特长与技能（专业技能）--->改为了职称
        LambdaQueryWrapper<CvSkills> skillsQuery = new LambdaQueryWrapper<>();
        skillsQuery.eq(CvSkills::getDelFlag, false).eq(CvSkills::getResumeId, resumeId)
                .orderByDesc(CvSkills::getId);
        List<CvSkills> skillsInfos = cvSkillsService.list(skillsQuery);
        result.setSkillsInfos(skillsConvert.toVO(skillsInfos));

        //资质证书
        LambdaQueryWrapper<CvCertificate> certificateQuery = new LambdaQueryWrapper<>();
        certificateQuery.eq(CvCertificate::getDelFlag, false).eq(CvCertificate::getResumeId, resumeId)
                .orderByDesc(CvCertificate::getTime);
        List<CvCertificate> certificateInfos = cvCertificateService.list(certificateQuery);
        result.setCertificateInfos(certificateConvert.toVO(certificateInfos));
        List<File> files=new ArrayList<>();
        if (StringUtils.isNotBlank(cvResume.getEnclosureId())){
            String[] strings = cvResume.getEnclosureId().split(",");
            for (String string : strings) {
                File file = fileService.getById(string);
                if (null != file){
                    files.add(file);
                }
            }

        }

        result.setFiles(files);

        return result;
    }


//    @Override
//    public cn.com.hhrcw.resumeDeliveryConfig.vo.ResumeInfoVO getResumeInfo2(String resumeId) {
//        cn.com.hhrcw.resumeDeliveryConfig.vo.ResumeInfoVO result = new cn.com.hhrcw.resumeDeliveryConfig.vo.ResumeInfoVO();
//        LambdaUpdateWrapper<CvResume> wrapper=Wrappers.lambdaUpdate();
//        wrapper.eq(CvResume::getId, resumeId)
//                .eq(CvResume::getDelFlag, false);
//
//        CvResume cvResume = resumeService.getOne(wrapper);
//        if (cvResume == null) {
//            return new cn.com.hhrcw.resumeDeliveryConfig.vo.ResumeInfoVO();
//        }
//        String userId = cvResume.getUserId();
//
//        LambdaQueryWrapper<PiInfo> infoQuery = new LambdaQueryWrapper<>();
//        infoQuery.eq(PiInfo::getDelFlag, false).eq(PiInfo::getUserId, userId);
//        PiInfo info = piInfoService.getOne(infoQuery);
//        result.setUserInfo(info);
//        //基本信息
//        result.setResumes(cvResume);
//
//        // 家庭成员
//        List<CvFamilyMember> list = cvFamilyMemberService.lambdaQuery().eq(CvFamilyMember::getDelFlag, false)
//                .eq(CvFamilyMember::getResumeId, resumeId).list();
//        result.setFamilyMembers(list);
//
//        //期望职位
//        LambdaQueryWrapper<CvWorkIntention> workIntentionQuery = new LambdaQueryWrapper<>();
//        workIntentionQuery.eq(CvWorkIntention::getDelFlag, false).eq(CvWorkIntention::getResumeId, resumeId)
//                .orderByDesc(CvWorkIntention::getCreateTime);
//        List<CvWorkIntention> workIntentions = cvWorkIntentionService.list(workIntentionQuery);
//        result.setWorkIntentions(workIntentions);
//
//        //工作实习经历
//        LambdaQueryWrapper<CvUserWorkInfo> userWorkInfoQuery = new LambdaQueryWrapper<>();
//        userWorkInfoQuery.eq(CvUserWorkInfo::getDelFlag, false).eq(CvUserWorkInfo::getResumeId, resumeId).orderByDesc(CvUserWorkInfo::getWorkStartTime);
//        List<CvUserWorkInfo> userWorkInfos = cvUserWorkInfoService.list(userWorkInfoQuery);
//        result.setUserWorkInfos(userWorkInfos);
//
//        //教育经历
//        LambdaQueryWrapper<CvUserEduInfo> userEduInfoQuery = new LambdaQueryWrapper<>();
//        userEduInfoQuery.eq(CvUserEduInfo::getDelFlag, false).eq(CvUserEduInfo::getResumeId, resumeId);
//        List<CvUserEduInfo> userEduInfos = cvUserEduInfoService.list(userEduInfoQuery);
//        result.setUserEduInfos(userEduInfos);
//
//        //项目经历
//        LambdaQueryWrapper<CvProjectInfo> projectInfoQuery = new LambdaQueryWrapper<>();
//        projectInfoQuery.eq(CvProjectInfo::getDelFlag, false).eq(CvProjectInfo::getResumeId, resumeId);
//        List<CvProjectInfo> projectInfos = cvProjectInfoService.list(projectInfoQuery);
//        result.setProjectInfos(projectInfos);
//
//        //培训经历
//        LambdaQueryWrapper<CvTrain> trainQuery = new LambdaQueryWrapper<>();
//        trainQuery.eq(CvTrain::getDelFlag, false).eq(CvTrain::getResumeId, resumeId).orderByAsc(CvTrain::getStartTime);
//        List<CvTrain> trainInfos = cvTrainService.list(trainQuery);
//        result.setTrainInfos(trainInfos);
//
//        //语言水平
//        LambdaQueryWrapper<CvLanguageLevel> languageLevelQuery = new LambdaQueryWrapper<>();
//        languageLevelQuery.eq(CvLanguageLevel::getDelFlag, false).eq(CvLanguageLevel::getResumeId, resumeId);
//        List<CvLanguageLevel> languageLevelInfos = cvLanguageLevelService.list(languageLevelQuery);
//        result.setLanguageLevelInfos(languageLevelInfos);
//
//        //职业特长与技能（专业技能）
//        LambdaQueryWrapper<CvSkills> skillsQuery = new LambdaQueryWrapper<>();
//        skillsQuery.eq(CvSkills::getDelFlag, false).eq(CvSkills::getResumeId, resumeId);
//        List<CvSkills> skillsInfos = cvSkillsService.list(skillsQuery);
//        result.setSkillsInfos(skillsInfos);
//
//        //资质证书
//        LambdaQueryWrapper<CvCertificate> certificateQuery = new LambdaQueryWrapper<>();
//        certificateQuery.eq(CvCertificate::getDelFlag, false).eq(CvCertificate::getResumeId, resumeId);
//        List<CvCertificate> certificateInfos = cvCertificateService.list(certificateQuery);
//        result.setCertificateInfos(certificateInfos);
//        List<File> files=new ArrayList<>();
//        if (StringUtils.isNotBlank(cvResume.getEnclosureId())){
//            String[] strings = cvResume.getEnclosureId().split(",");
//            for (String string : strings) {
//                File file = fileService.getById(string);
//                if (null != file){
//                    files.add(file);
//                }
//            }
//        }
//        result.setFiles(files);
//        return result;
//    }

}
